﻿using System;
using System.ComponentModel;
using System.Web.UI;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.Core;

namespace Yuanlin.Business
{
    public class SpecialNewsBreadCrumb : Literal
    {
        protected Queue crumbs = new Queue();
        protected HtmlAnchor anchor;
        private int entityId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.SpecialNewsId);
        private int provinceId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.ProvinceId);
        private int cityId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.CityId);
        private int townId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.TownId);
        private int smallTownId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.SmallTownId);
        private int categoryType = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.CategoryType);
        private int kind = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.Kind);

        protected override void CreateChildControls()
        {
            string title = "";
            if (categoryType == (int)CategoryType.ZhiXiangNews)
            {
                title = ResourceManager.GetString("huamuzhixiang");
            }
            else if (categoryType == (int)CategoryType.ShiChangNews)
            {
                title = ResourceManager.GetString("huamushichang");
            }
            else if (categoryType == (int)CategoryType.HuiZhanNews)
            {
                title = ResourceManager.GetString("huamuhuizhan");
            }
            crumbs.Enqueue(GetAnchor(title+"首页", GetNewsListUrl(0, 0, 0, 0)));
            if (entityId > 0)
            {
                SpecialNews news = BusinessManager.GetSpecialNews(entityId);
                if (news != null)
                {
                    Area c1 = null;
                    Area c2 = null;
                    Area c3 = null;
                    Area c4 = null;

                    c1 = BusinessManager.GetArea(news.Province.Value);
                    c2 = BusinessManager.GetArea(news.City.Value);
                    c3 = BusinessManager.GetArea(news.Town.Value);
                    c4 = BusinessManager.GetArea(news.SmallTown.Value);

                    if (c1 != null && c2 != null && c3 != null && c4 != null)
                    {
                        crumbs.Enqueue(GetAnchor(c1.Title.Value, GetNewsListUrl(c1.EntityId.Value, 0, 0, 0)));
                        crumbs.Enqueue(GetAnchor(c2.Title.Value, GetNewsListUrl(c1.EntityId.Value, c2.EntityId.Value, 0, 0)));
                        crumbs.Enqueue(GetAnchor(c3.Title.Value, GetNewsListUrl(c1.EntityId.Value, c2.EntityId.Value, c3.EntityId.Value, 0)));
                        crumbs.Enqueue(GetAnchor(c4.Title.Value, GetNewsListUrl(c1.EntityId.Value, c2.EntityId.Value, c3.EntityId.Value, c4.EntityId.Value)));
                    }

                    crumbs.Enqueue(GetAnchor(news.Title.Value, GetNewsUrl(entityId)));
                }
            }
            else//主要在default.aspx中显示
            {
                Area c1 = null;
                Area c2 = null;
                Area c3 = null;
                Area c4 = null;

                c1 = BusinessManager.GetArea(provinceId);
                if (c1 == null)
                    return;

                c2 = BusinessManager.GetArea(cityId);
                if (c2 == null)
                {
                    crumbs.Enqueue(GetAnchor(c1.Title.Value, GetNewsListUrl(c1.EntityId.Value, 0, 0, 0)));
                    return;
                }

                c3 = BusinessManager.GetArea(townId);
                if (c3 == null)
                {
                    crumbs.Enqueue(GetAnchor(c1.Title.Value, GetNewsListUrl(c1.EntityId.Value, 0, 0, 0)));
                    crumbs.Enqueue(GetAnchor(c2.Title.Value, GetNewsListUrl(c1.EntityId.Value, c2.EntityId.Value, 0, 0)));
                    return;
                }

                c4 = BusinessManager.GetArea(smallTownId);
                if (c4 == null)
                {
                    crumbs.Enqueue(GetAnchor(c1.Title.Value, GetNewsListUrl(c1.EntityId.Value, 0, 0, 0)));
                    crumbs.Enqueue(GetAnchor(c2.Title.Value, GetNewsListUrl(c1.EntityId.Value, c2.EntityId.Value, 0, 0)));
                    crumbs.Enqueue(GetAnchor(c3.Title.Value, GetNewsListUrl(c1.EntityId.Value, c2.EntityId.Value, c3.EntityId.Value, 0)));
                    return;
                }
                crumbs.Enqueue(GetAnchor(c1.Title.Value, GetNewsListUrl(c1.EntityId.Value, 0, 0, 0)));
                crumbs.Enqueue(GetAnchor(c2.Title.Value, GetNewsListUrl(c1.EntityId.Value, c2.EntityId.Value, 0, 0)));
                crumbs.Enqueue(GetAnchor(c3.Title.Value, GetNewsListUrl(c1.EntityId.Value, c2.EntityId.Value, c3.EntityId.Value, 0)));
                crumbs.Enqueue(GetAnchor(c4.Title.Value, GetNewsListUrl(c1.EntityId.Value, c2.EntityId.Value, c3.EntityId.Value, c4.EntityId.Value)));
            }
        }
        private string GetNewsListUrl(int provinceId, int cityId, int townId, int smallTownId)
        {
            if (categoryType == (int)CategoryType.ZhiXiangNews)
            {
                return SiteUrls.Instance().GetHuamuZhiXiangNewsListUrl(provinceId, cityId, townId, smallTownId);
            }
            else if (categoryType == (int)CategoryType.ShiChangNews)
            {
                return SiteUrls.Instance().GetHuamuShichangNewsListUrl(provinceId, cityId, townId, smallTownId);
            }
            return SiteUrls.Instance().GetHuamuHuizhanNewsListUrl(provinceId, cityId, townId, smallTownId);
        }
        private string GetNewsUrl(int entityId)
        {
            if (categoryType == (int)CategoryType.ZhiXiangNews)
            {
                return SiteUrls.Instance().GetHuamuZhiXiangNewsUrl(entityId);
            }
            else if (categoryType == (int)CategoryType.ShiChangNews)
            {
                return SiteUrls.Instance().GetHuamuShichangNewsUrl(entityId);
            }
            return SiteUrls.Instance().GetHuamuHuizhanNewsUrl(entityId);
        }
        protected Control GetAnchor(string innerText, string href)
        {
            anchor = new HtmlAnchor();
            anchor.InnerHtml = innerText;
            anchor.Attributes["class"] = "crumbAnchor";
            anchor.HRef = href;

            return anchor;
        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write("您的位置:&nbsp;&nbsp;");

            while (crumbs.Count > 0)
            {
                HtmlAnchor a = (HtmlAnchor)crumbs.Dequeue();

                a.Title = a.InnerHtml;

                if ((crumbs.Count > 0) && (a.InnerHtml.Length > 30))
                {
                    a.InnerHtml = a.InnerHtml.Substring(0, 30) + "...";
                }
                a.RenderControl(writer);

                if (crumbs.Count > 0)
                {
                    writer.Write(ResourceManager.GetString("BreadCrumb_Seperator"));
                }
            }

        }

    }
}